﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Mvc;
using YouErYuanBLL;
using YouErYuanModels;

namespace YouErYuanManage.Controllers
{
    public class TongzhiController : Controller
    {

        [YouErYuanManage.Filter.IsLogin]
        public string InsertPublish(string sourse, string title, string contentWZ, string coverimgurl)
        {
            //解码
            //byte[] jmBytes = Convert.FromBase64String(contentWZ);
            //string decodeContent = Encoding.UTF8.GetString(jmBytes);
            TongzhiPubsh model = new TongzhiPubsh();
            model.Title = title;
            model.Source = sourse;
            model.Contentwz = contentWZ;
            //model.Coverimgurl = coverimgurl;//封面图片
            Teacher teacher = HttpContext.Session["user"] as Teacher;
            model.Schoolid = teacher.Schoolid;
            int count = new TongzhiPublishBLL().Add(model);
            if (count==1)
            {
                //过滤html
                string msg = "";// CommonLib.CommonMethod.ClearHtml(decodeContent);
                int length = contentWZ.Length >= 35 ? 35 : contentWZ.Length;
                msg = contentWZ.Substring(0, length);
                title = "[宝贝家园]" + title;
                HttpConnectToServer(title, msg);//推送通知
            }
            return CommonLib.CommonMethod.FormatJsonObj(count, "");
        }

        /// <summary>
        /// POST方式请求Webservice方法，地址："http://localhost:5320/WebService1.asmx/HelloWorld"，其中HelloWorld为请求的web方法
        /// </summary>
        /// <param name="url">请求的web服务地址</param>
        /// <param name="sendData">发送的数据</param>
        /// <returns></returns>
        private string HttpConnectToServer(string title, string contentWZ)
        {
            //保存
            Teacher teacher = HttpContext.Session["user"] as Teacher;
            string msg = contentWZ;
            string tag = teacher.Schoolid + "JiaZhang";//推送给全园家长

            //发送消息
            //{"title":"宝贝家园","msg":"发送的消息","tag":"9999"} 

            //标题编码
            byte[] bytesTitle = Encoding.UTF8.GetBytes(title);
            title = Convert.ToBase64String(bytesTitle);
            //消息编码
            byte[] bytesmsg = Encoding.UTF8.GetBytes(msg);
            msg = Convert.ToBase64String(bytesmsg);
            //发送数据编码
            string datastr = "{'title':'" + title + "','msg':'" + msg + "','tag':'" + tag + "'}";
            byte[] bytes2 = Encoding.UTF8.GetBytes(datastr);
            datastr = Convert.ToBase64String(bytes2);

            string url = "http://139.129.43.143:8086/Handler1.ashx?";
            string postData = "method=TagsDevice&appType=1&pushType=1&data=" + datastr;          
            byte[] dataArray = System.Text.Encoding.UTF8.GetBytes(postData);
            //创建请求
            System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.HttpWebRequest.Create(url);
            request.Method = "POST";
            request.ContentLength = dataArray.Length;
            request.ContentType = "application/x-www-form-urlencoded";
            //创建输入流
            System.IO.Stream dataStream = null;
            try
            {
                dataStream = request.GetRequestStream();
            }
            catch (Exception)
            {
                return null;//连接服务器失败
            }
            //发送请求
            dataStream.Write(dataArray, 0, dataArray.Length);
            dataStream.Close();
            //读取返回消息
            string res = string.Empty;
            try
            {
                System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
                System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);
                res = reader.ReadToEnd();
                reader.Close();
            }
            catch (Exception)
            {
                return null;//连接服务器失败
            }
            return res;
        }
    }
}